package com.wry.wry_test.feign.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import java.lang.reflect.InvocationTargetException;

/**
 * @Author: wangrongyi
 * @Date: 2024/7/24 16:16
 * @Description: 负载均衡算法注入配置
 */
@Configuration
@Slf4j
public class LoadBalanceConfig {

    @Bean
    public LoadBalanceService loadBalanceService(LoadBalanceProperties loadBalanceProperties) {
        String className = loadBalanceProperties.getAlgorithm();
        // 反射加载负载均衡算法类
        try {
            Class<?> clazz = Class.forName(className);
            return (LoadBalanceService) clazz.getConstructor(LoadBalanceProperties.class).newInstance(loadBalanceProperties);
        } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException |
                 InvocationTargetException e) {
            log.error("算法注入失败 class path = {}", className);
            throw new RuntimeException(e);
        }
    }
}
